package com.novabluetooth.services.servicedevices;

import android.bluetooth.BluetoothGattService;
import android.util.Log;

import com.novabluetooth.services.TJBluetoothLeService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by John on 2018/3/29.
 */

public class WeiTaiXueTangService extends TJBluetoothLeService {

    private String sino_readData = "2D2D0000100080C60105000084BF020100002D2D";
//    private String sino_closeBle = "534E0600040C000016";
    private String sino_closeData = "2D2D00000F00C1CB01057DE993930001012D2D";
    public final static UUID serviceSanNuoUUID = UUID.fromString("0000FFE0-0000-1000-8000-00805F9B34FB");
    public final static UUID writeSanNuoCharactUUID = UUID.fromString("0000FFE1-0000-1000-8000-00805F9B34FB");
    public final static UUID readSanNuoCharactUUID = UUID.fromString("0000FFE1-0000-1000-8000-00805F9B34FB");
    @Override
    public void servicesDiscovered() {
        super.servicesDiscovered();
        BluetoothGattService gattService = mBluetoothGatt.getService(serviceSanNuoUUID);
        writeGattCharact = gattService.getCharacteristic(writeSanNuoCharactUUID);
        readGattCharact = gattService.getCharacteristic(readSanNuoCharactUUID);
        mBluetoothGatt.setCharacteristicNotification(readGattCharact, true);
        if(writeGattCharact!=null&&readGattCharact!=null){
            ifSuccess = writeDataToDevice(sino_readData);
        }

    }
    byte[] buffer1=new byte[20];
    byte[] buffer2=new byte[20];
    byte[] buffer3=new byte[16];
   List list=new ArrayList();
    @Override
    public boolean isRightData(byte[] buffer) {
        super.isRightData(buffer);
        int readSize = buffer.length;
        Log.e("获取到数据了",readSize+"************");
        if (readSize <= 0) {

            ifSuccess = false;
        } else if (readSize < 15) {
            Log.e("LeeTag", "Data is Short");
            ifSuccess = false;
        } else if(readSize==20){
            if(buffer1.length==0){
                buffer1=buffer;
            }else{
                buffer2=buffer;
            }
            ifSuccess = false;
        }else if(readSize==16){
            buffer3=buffer;
            for (int i = 2+6+6+2; i < buffer1.length-2; i++)
            {
                list.add(buffer1[i]);
            }
            for (int i = 2 + 6; i < buffer2.length - 2; i++)
            {
                list.add(buffer2[i]);
            }

            for (int i = 2 + 6; i < buffer3.length - 2; i++)
            {
                list.add(buffer3[i]);
            }

            int d = (byte)list.get(9);
            int v = ( (byte)list.get(10) & 0xFF) << 8 |  (byte)list.get(9); ;

            float t = (float)(v)/18.0f;
            int   scale  =   1;//设置位数
            int   roundingMode  =  4;//表示四舍五入，可以选择其他舍值方式，例如去尾，等等.
            BigDecimal bd  =   new  BigDecimal(t);
            bd   =  bd.setScale(scale,roundingMode);
            float ft   =  bd.floatValue();
            // checkData = "血糖:" + bg;
            checkData = String.valueOf(ft);
            ifSuccess = true ;
        }
        return ifSuccess;

    }

    @Override
    public void closeDevice() {
        super.closeDevice();
        ifSuccess = writeDataToDevice(sino_closeData);
//        writeDataToDevice(sino_closeBle);
    }
}
